/*
 * Solo - A small and beautiful blogging system written in Java.
 * Copyright (c) 2010-present, b3log.org
 *
 * Solo is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.b3log.solo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.service.ServiceException;
import org.b3log.solo.model.Article;
import org.b3log.solo.model.Common;
import org.b3log.solo.processor.FetchUploadProcessor;
import org.b3log.solo.util.Markdowns;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class AutoInport extends AbstractTestCase {
    public static void main(String[] args) {

    }

    @Test
    public void test02() {
//        String s = "图1.1网络应用/服务](Chapter1-Fundamentals.assets/Figure_1_1.png";
//        String suffix = FileNameUtil.getSuffix(s);
//        String prefix = FileNameUtil.getPrefix(s);
//        String extName = FileNameUtil.getName(s);
//        System.out.println("extName = " + extName);
//        System.out.println("prefix = " + prefix);
//        System.out.println("suffix = " + suffix);

        String path = "D:\\WorkSpace\\GitHub\\Vert.x-in-Action-ChineseVersion\\";
        File[] files = new File(path).listFiles();
        List fileList = Arrays.asList(files);
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                return o1.getName().compareTo(o2.getName());
            }
        });

        fileList.forEach(System.out::println);
    }

    @SneakyThrows
    @Test
    public void test1() {
        String path = "D:\\Work_space\\github\\Vert.x-in-Action-ChineseVersion\\";
    }

    @NotNull
    private JSONObject getJsonObject(String article1Title, String path) {
        final JSONObject requestJSONObject = new JSONObject();
        final JSONObject article = new JSONObject();
        requestJSONObject.put(Article.ARTICLE, article);

        final String userId = getUserQueryService().getAdmin().optString(Keys.OBJECT_ID);

        article.put(Article.ARTICLE_AUTHOR_ID, userId);
        article.put(Article.ARTICLE_TITLE, article1Title);
        article.put(Article.ARTICLE_ABSTRACT, "");

        String s = FileUtil.readUtf8String(path);

        article.put(Article.ARTICLE_CONTENT, s);
        article.put(Article.ARTICLE_TAGS_REF, "pdf");
        article.put(Article.ARTICLE_PERMALINK, "");
        article.put(Article.ARTICLE_STATUS, Article.ARTICLE_STATUS_C_PUBLISHED);
        article.put(Common.POST_TO_COMMUNITY, false);
        article.put(Article.ARTICLE_SIGN_ID, "1");
        article.put(Article.ARTICLE_VIEW_PWD, "");
        return requestJSONObject;
    }

    @Test
    public void tes03() throws IOException, ServiceException {

        String path = "D:\\WorkSpace\\GitHub\\Vert.x-in-Action-ChineseVersion\\";
        for (File listFile : new File(path).listFiles()) {
            String absolutePath = listFile.getAbsolutePath();
            if (listFile.isFile() && absolutePath.lastIndexOf(".md") > 0) {

                String name = FileNameUtil.mainName(absolutePath);
                final JSONObject resData = getJsonObject(name, absolutePath);

                String md = resData.getJSONObject(Article.ARTICLE).getString(Article.ARTICLE_CONTENT);
                String[] link = StringUtils.substringsBetween(md, "![", ")");
                for (int i = 0; i < link.length; i++) {

                    String[] url = link[i].split("]\\(");
                    if (url[1].indexOf(".assets") > 0) {

                        System.out.println("url = " + url[1]);
                        File file = new File(path + url[1]);
                        System.out.println("file.exists() = " + file.exists());
                        if (file.exists()) {
                            String name1 = FileNameUtil.getName(file);

                            JSONObject res = getFetchUploadService().getJsonObject(name1, FileUtil.readBytes(file));
                            JSONObject succMap = res.getJSONObject("succMap");
                            String key = succMap.keys().next();
                            String value = succMap.getString(key);
                            String format = String.format("![%s](%s)", url[0], value);
                            System.out.println("res = " + format);
                            md = md.replace("![" + link[i] + ")", format);

                        }
                    }
                }
                resData.getJSONObject(Article.ARTICLE).put(Article.ARTICLE_CONTENT, md);

                System.out.println(name);
                getArticleMgmtService().addArticle(resData);

            }
        }
    }
}
